//
//  AddCardViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/21.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "AddCardViewController.h"
#import "AddCardTableViewCell.h"
#import "AddCardfooterCell.h"
#import "AddViewController.h"

#import "MyBankCardModel.h"

@interface AddCardViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)  NSMutableArray *addCardArr;
@end

@implementation AddCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的银行卡";
    [self.view addSubview:self.addCardTableView];
    [self addCardTableView];
  
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(notice:) name:@"refresh" object:nil];
    
    [self getSerhUserList];
}

//刷新tableView
-(void)notice:(id)sender{
    
//    [self myBankCardRequest];
    [_addCardTableView reloadData];
//    self.view.backgroundColor = [UIColor greenColor];
    
}

- (UITableView *)addCardTableView{
    
    if (!_addCardTableView) {
        _addCardTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height) style:UITableViewStylePlain];
        _addCardTableView.delegate = self;
        _addCardTableView.dataSource = self;
        _addCardTableView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00];
        _addCardTableView.separatorStyle = NO;

    }
    
    return _addCardTableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _addCardArr.count+1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 8;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if(indexPath.section == _addCardArr.count){
        AddCardfooterCell *footerCell = [tableView dequeueReusableCellWithIdentifier:@"FOOTERTABLEVIEWCELL"];
        if (footerCell == nil) {
            footerCell = [[[NSBundle mainBundle] loadNibNamed:@"AddCardfooterCell" owner:nil options:nil] lastObject];
        }
        footerCell.selectionStyle = UITableViewCellSelectionStyleNone;
        footerCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return footerCell;
    }
    AddCardTableViewCell *addCardCell = [tableView dequeueReusableCellWithIdentifier:@"ADDCARDTABLEVIEWCELL"];
    if (addCardCell == nil) {
        addCardCell = [[[NSBundle mainBundle] loadNibNamed:@"AddCardTableViewCell" owner:nil options:nil] lastObject];
    }
    addCardCell.selectionStyle = UITableViewCellSelectionStyleNone;
    MyBankCardModel *model = _addCardArr[indexPath.section];
    [addCardCell setDataWithModel:model];
    return addCardCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == _addCardArr.count) {
        return 40;
    }
    return 90;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /**
     *  最后一组 添加银行按钮
     */
    if (indexPath.section == _addCardArr.count) {
        AddViewController *addView = [[AddViewController alloc] init];
        [self.navigationController pushViewController:addView animated:YES];
    }else{
        
        MyBankCardModel *model = _addCardArr[indexPath.section];
        
        _BLOCK(model);
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//查询银行卡列表信息
-(void)getSerhUserList{
    NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    NSDictionary *para = @{
                           @"PageIndex":@"0",
                           @"LoginId":loginId
                           };
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Users.GetSerhUserList" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } progress:nil];
    
}


@end
